// pages/index/parkingplace/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iosDialog1: false,
    istrue: false
  },
  close: function () {
    this.setData({
      iosDialog1: false,
      iosDialog2: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      url: getApp().data.servser + '/parking/place/detail',
      data: {
        seqId: options.id
      },
      success: function (res) {
        that.setData({
          parkingPlace: res.data.data.parkingPlaceUseInfo,
          now: res.data.data.now,
          order_time: res.data.data.now
        })
      }
    })
  },
  useLater: function () {
    var that = this;
    let order_time = (this.data.order_time)
    if (order_time == '点我选择预约时间，立即降锁无需选择。') {
      wx.showToast({
        title: '请选择预约时间!',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        url: getApp().data.servser + '/parking/place/use/order',
        method: 'POST',
        data: {
          orderTime: that.data.order_time,
          parkingPlaceId: that.data.parking_place_id,
          parkingId: that.data.parking_id,
          openId: getApp().globalData.openid,
          payFlag: '1'
        },
        success: function (res) {
          if (res.data.code == 500) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '车位预约成功!',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              istrue: false
            })
            wx.reLaunch({
              url: '../parking?id=' + that.data.parking_id,
            })
          }
        }
      })
    }
  },
  useNow: function () {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      url: getApp().data.servser + '/parking/place/use/use/now',
      method: 'POST',
      data: {
        parkingPlaceId: that.data.parking_place_id,
        parkingId: that.data.parking_id,
        openId: getApp().globalData.openid,
        payFlag: '1'
      },
      success: function (res) {
        if (res.data.code == 500) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '车位降锁成功!',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            istrue: false
          })
          wx.reLaunch({
            url: '../parking?id=' + that.data.parking_id,
          })
        }
      }
    })
  },
  beginUse: function () {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      url: getApp().data.servser + '/parking/place/use/begin/use',
      method: 'POST',
      data: {
        parkingPlaceId: that.data.parking_place_id,
        parkingId: that.data.parking_id,
        openId: getApp().globalData.openid
      },
      success: function (res) {
        wx.showToast({
          title: '车位降锁成功!',
          icon: 'none',
          duration: 2000
        })
        wx.reLaunch({
          url: '../parking?id=' + that.data.parking_id,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  goCar: function (e) {
    wx.navigateTo({
      url: '../../user/car',
    })
    this.setData({
      iosDialog1: false
    })
  },
  parktap: function (e) {
    let that = this
    // check黑名单
    wx.request({
      method: "GET",
      url: getApp().data.servser + '/wx/mini/user/detail',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        openId: getApp().globalData.openid
      },
      success: function (res) {
        if (res.data.data.wxMiniUser.status == '1') {
          that.setData({
            iosDialog2: true
          })
        } else {
          // check是否有车牌
          wx.request({
            method: "GET",
            url: getApp().data.servser + '/car/list',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            data: {
              openId: getApp().globalData.openid
            },
            success: function (res) {
              if (res.data.data.cars.length == 0) {
                that.setData({
                  iosDialog1: true
                })
              } else {
                that.setData({
                  istrue: true,
                  car_out: 'off',
                  cancleOrBegin: 'off',
                  parking_place_id: e.currentTarget.dataset.id,
                  parking_id: e.currentTarget.dataset.parkingid
                })
              }
            }
          })
        }
      }
    })


  },
  closeDialog: function () {
    this.setData({
      istrue: false
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      order_time: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})